package com.boarsoft.boar.soagov.svc.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.entity.ComInfo;
import com.boarsoft.boar.entity.SvcInfo;
import com.boarsoft.boar.soagov.com.ComBiz;
import com.boarsoft.boar.soagov.svc.SvcBiz;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.common.util.JsonUtil;
import com.boarsoft.flow.store.SimpleFlowStore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@RestController
@RequestMapping("/svc")
public class SvcAction {
	@Autowired
	private SvcBiz svcBiz;
	@Autowired
	private ComBiz comBiz;
	@Autowired
	private SimpleFlowStore flowStore;

	@JsonIgnoreProperties({ "face" })
	@RequestMapping("/get.do")
	public ReplyInfo<Object> get(String id) {
		SvcInfo o = svcBiz.get(id);
		return new ReplyInfo<Object>(true, o);
	}

	@RequestMapping("/save")
	public ReplyInfo<Object> save(SvcInfo f, ComInfo c) {
		svcBiz.save(f);
		// 无论这个服务是否真的有流程，都保存一个对应的SUBFLOW组件
		// 除非这个SUBFLOW在其它流程中被显示引用，否则这个组件在树上看不到
		comBiz.save(c);
		return ReplyInfo.SUCCESS;
	}

	@RequestMapping("/list.do")
	public ReplyInfo<Object> list(String key, String orderBy, int pageNo, int pageSize) {
		PagedResult<SvcInfo> pr = svcBiz.list(null, key, orderBy, pageNo, pageSize);
		return new ReplyInfo<Object>(true, JsonUtil.from(pr, "face"));
	}

	@RequestMapping("/delete.do")
	public ReplyInfo<Object> delete(String key) {
		String[] aa = key.split(",");
		for (String id : aa) {
			ReplyInfo<Object> ri = svcBiz.delete(id);
			if (ri.isSuccess()) {
				flowStore.delete(id);
			} else {
				return ri;
			}
		}
		return ReplyInfo.SUCCESS;
	}
}